package br.com.gbvbahia.mybooks.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.commons.modelos.EntityInterface;
import br.com.gbvbahia.commons.utils.AndroidUtils;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.dao.LivroDAO;
import br.com.gbvbahia.mybooks.modelos.LivroVO;

public class AvaliarFragment extends FragmentMae {
	private RatingBar rating;
	private TextView textNome;
	private EditText textAvaliacao;
	private CheckBox checkBox;
	private LivroDAO livroDAO;
	private LivroVO livro;
	private Button cancelar;
	private Button salvar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		livroDAO = new LivroDAO(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.frag_avaliar, null);
		rating = (RatingBar) view.findViewById(R.id.ratingBarAvaliacaoNota);
		textNome = (TextView) view.findViewById(R.id.tNomeInsert);
		textAvaliacao = (EditText) view
				.findViewById(R.id.inputAvaliarAvaliacao);
		checkBox = (CheckBox) view.findViewById(R.id.inputRecomendado);
		cancelar = (Button) view.findViewById(R.id.btnHeaderCancelar);
		salvar = (Button) view.findViewById(R.id.btnHeaderOk);

		rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				if (fromUser) {
					if (livro != null) {
						livro.setNota(Float.valueOf(ratingBar.getRating())
								.intValue());
						ratingBar.setRating(livro.getNota());
					} else {
						ratingBar.setRating(0F);
						alert(getString(R.string.aviso_16));
					}
				}
			}
		});

		cancelar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getActivity().finish();
			}
		});

		salvar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (livro == null) {
					alert(getString(R.string.aviso_16));
					return;
				}
				if (livro.getNota() == -1) {
					alert(getString(R.string.aviso_17));
					return;
				}

				livro.setAvaliacao(textAvaliacao.getText().toString());
				livro.setRecomendado(checkBox.isChecked() ? EntityInterface.TRUE
						: EntityInterface.FALSE);
				if (!livro.seValidar().isEmpty()) {
					for (Integer aviso : livro.seValidar()) {
						Toast.makeText(getActivity(), getString(aviso),
								Toast.LENGTH_SHORT).show();
					}
					return;
				}
				startTransacao(new Transacao() {
					@Override
					public void executar() throws Exception {
						livroDAO.update(livro);
					}

					@Override
					public void atualizarView() {
						setLivro(null);
						FragmentTransfer ft = (FragmentTransfer)getActivity();
						ft.updateFragment(R.layout.frag_avaliar, null);
						Toast.makeText(getActivity(),
								getString(R.string.aviso_18),
								Toast.LENGTH_SHORT).show();
					}
				});
				rating.requestFocus();
				AndroidUtils.closeVirtualKeyboard(getActivity(), rating);
			}
		});

		return view;
	}

	public void setLivro(LivroVO livro) {
		this.livro = livro;
		if (livro != null) {
			if (livro.getNota() >= 0) {
				rating.setRating(livro.getNota());
			}else {
				rating.setRating(0F);
			}
			if (livro.getRecomendado() == EntityInterface.TRUE) {
				checkBox.setChecked(true);
			} else {
				checkBox.setChecked(false);
			}
			textAvaliacao.setText(livro.getAvaliacao());
			textNome.setText(livro.getNomeLivro());
		} else {
			rating.setRating(0F);
			textNome.setText("");
			textAvaliacao.setText("");
			checkBox.setChecked(false);
		}
	}
}
